var lineName = "";
var station = '';
var address = "";
var container = document.getElementById("container");
var isFrist = true;
var order = true;
(function(e) {
	e('.mui-scroll-wrapper').scroll({
		indicators: true //是否显示滚动条
	});

	map = new AMap.Map("container", {
		resizeEnable: true,
		center: centerPoint, //地图中心点
		zoom: 13 //地图显示的缩放级别
	})

	station = getQueryString("id");
	address = getQueryString("address");
	$("#map").click(function() {
		if(isShow(container)) {
			hiddenEle(container);
			$(".line-info").show();
		} else {
			if(isFrist) {
				lineSearch(lineName , order);
				isFrist = false;
			}

			showEle(container);
			$(".line-info").hide();
		}
	});
	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	mui.back = function(event) {
		if(isShow(container)) {
			hiddenEle(container);
			$(".line-info").show();
		} else {
			history.go(-1);
		}
		return false;
	};

	if(isEmpty(address)) {
		AMap.service(["AMap.PlaceSearch"], function() {
			var placeSearch = new AMap.PlaceSearch({ //构造地点查询类
				pageSize: 5,
				pageIndex: 1,
				city: "南京", //城市
				map: map
			});
			//关键字查询
			placeSearch.search(station, function(status, result) {
				if(status == 'complete') {
					console.log(JSON.stringify(result));
					var poiList = result.poiList;
					if(isEmpty(poiList) || isEmpty(poiList.pois) || poiList.pois.length == 0) {
						mui.toast("暂无站点信息");
						return;
					}
					var pois = poiList.pois;
					var str = pois[0].address;
					console.log(str);
					setStation(str);
				} else {
					mui.toast("暂无站点信息！");
				}
			});
		});
	} else {
		setStation(address);
	}

})(mui);

function setStation(address) {
	var lins = address.split(';');

	$('.station-name').text(station);

	if(!isEmpty(lins)) {
		lineName = lins[0];
	}
	for(var i = 0; i < lins.length; i++) {
		if(i == 0) {
			$(".herder").append('<li class="mui-active">' + lins[i] + '</li>')
		} else {
			$(".herder").append('<li>' + lins[i] + '</li>')
		}
	}

	station = station.substring(0, station.length - 5);
	console.log('station==>' + station);
	var leftSize = $(".herder li:last-child").offset().left;
	var lastWidth = $(".herder li:last-child").width();
	var herderWidth = leftSize + lastWidth + 30;
	console.log(herderWidth);
	$(".herder").css('width', herderWidth + 'px')

	$(".herder li").click(function() {
		$(".herder li").attr('class', "");
		$(this).attr('class', "mui-active");
		lineName = this.innerHTML;
		lineSearch(lineName , order);
	});

	lineSearch(lineName , order);

	$(".bus-change").click(function() {
		order = !order;
		lineSearch(lineName , order);
	});

}

var map;
/*公交线路查询*/
function lineSearch(name, isOorder) {
	//实例化公交线路查询类，只取回一条路线
	console.log(name)

	if(isEmpty(lineName)) {
		mui.toast("获取线路失败");
		return;
	}

	var linesearch = new AMap.LineSearch({
		pageIndex: 1,
		city: city,
		pageSize: 1,
		extensions: 'all'
	});
	linesearch.search(name, function(status, result) {
		if(status === 'complete' && result.info === 'OK') {
			lineSearch_Callback(result, isOorder);
		} else {
			alert(result);
		}
	});
}
/*公交路线查询服务返回数据解析概况*/
function lineSearch_Callback(data, isOorder) {
	var lineArr = data.lineInfo;
	var lineNum = data.lineInfo.length;
	if(lineNum == 0) {} else {
		for(var i = 0; i < lineNum; i++) {
			var pathArr = lineArr[i].path;
			var stops = lineArr[i].via_stops;
			var startPot = isOorder ? stops[0].location : stops[stops.length - 1].location;
			var endPot = isOorder ?stops[stops.length - 1].location : stops[0].location;
			if(i == 0) {
				console.log(JSON.stringify(stops));
				drawbusLine(startPot, stops, endPot, pathArr , isOorder);
			}
		}
	}
}
/*绘制路线*/
function drawbusLine(startPot, stations, endPot, BusArr , isOorder) {

	var startStation = isOorder?stations[0]:stations[stations.length - 1];
	var endStation = isOorder?stations[stations.length - 1]:stations[0];
	console.log(startStation);
	$('.line-title .start').text(startStation.name);
	$('.line-title .end').text(endStation.name);

	map.clearMap();

	//绘制起点，终点
	new AMap.Marker({
		map: map,
		position: [startPot.lng, startPot.lat], //基点位置
		icon: "https://webapi.amap.com/theme/v1.3/markers/n/start.png",
		zIndex: 10
	});
	console.log(stations.length);
	for(var i = 0; i < stations.length; i++) {

		stations[i].stutus = "未发车";
		stations[i].time = 5;
		stations[i].carNo = '苏A85230';
		stations[i].carNum = 2;

		if(i == 0 || i == stations.length - 1) {
			continue;
		}

		var lng = stations[i].location.lng;
		var lat = stations[i].location.lat;
		new AMap.Marker({
			map: map,
			position: [lng, lat], //基点位置
			icon: '../../ui/home/images/bus.png',
			offset: new AMap.Pixel(-10, -10),
			zIndex: 10
		});
	}

	console.log(stations.length);
	mui('.mui-scroll-wrapper').scroll().scrollTo(0, 0, 100);
	removeLi("stationList");
	for(var i = 0; i < stations.length; i++) {
		createStation(stations[i] , isOorder)
	}
	new AMap.Marker({
		map: map,
		position: [endPot.lng, endPot.lat], //基点位置
		icon: "https://webapi.amap.com/theme/v1.3/markers/n/end.png",
		zIndex: 10
	});
	//绘制乘车的路线
	busPolyline = new AMap.Polyline({
		map: map,
		path: BusArr,
		strokeColor: "#1199ff", //线颜色
		strokeOpacity: 0.9, //线透明度
		strokeWeight: 6 //线宽
	});
	map.setFitView();
}

function createStation(row , isOorder) {
	var active = "";
	if(row.name.indexOf(station) > -1) {
		active = 'mui-active';
	}

	var htmlStr = '<li class="mui-table-view-cell mui-collapse ' + active + '">';
	htmlStr += '<a class="mui-navigate-right" href="#">' + row.name + '</a>';
	htmlStr += '<div class="mui-collapse-content">';
	htmlStr += '<p>状态：第' + row.carNum + '辆车 <span class="num">' + row.stutus + '</span></p>';
	htmlStr += '<p>时间：' + row.time + '分钟后到站</p>';
	htmlStr += '<p>车牌号：' + row.carNo + '</p>';
	htmlStr += '</div>';
	htmlStr += '</li>';
	
	if(isOorder){
		$("#stationList").append(htmlStr);
	} else {
		$("#stationList").prepend(htmlStr);
	}
	
	
}